<template>
  <div class="header-info">
    <div class="operation">
      <span>
        <el-icon><bell /></el-icon>
      </span>
      <span>
        <span class="dot"></span>
        <el-icon><ChatDotRound /></el-icon>
      </span>
      <span>
        <el-icon><Postcard /></el-icon>
      </span>
    </div>
    <div class="info">
      <el-dropdown>
        <span class="user-info">
          <el-avatar :size="40" class="avatar" :src="defaultImg" />
          <span class="name">{{ userName }}</span>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="handleExitClick">
              <el-icon>
                <CircleCloseFilled />
              </el-icon>
              退出系统
            </el-dropdown-item>
            <el-dropdown-item divided>
              <el-icon>
                <InfoFilled />
              </el-icon>
              个人信息
            </el-dropdown-item>
            <el-dropdown-item>
              <el-icon>
                <Unlock />
              </el-icon>
              修改密码
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { storage } from '@/utils/storage-util';
import { LOGIN_TOKEN } from '@/global/constants';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { useLoginStore } from '@/store/login';
import { storeToRefs } from 'pinia';

const loginStore = useLoginStore();
const { userInfo } = storeToRefs(loginStore);

const userName = ref(userInfo.value?.name);

const defaultImg = ref(
  'https://yoshiyama-picture-for-typora.oss-cn-chengdu.aliyuncs.com/img_for_md/202403101348948.png'
);

const router = useRouter();

function handleExitClick() {
  storage.remove(LOGIN_TOKEN);
  ElMessage.success('退出成功！');
  router.push('/login');
}
</script>

<style scoped lang="scss">
@import '@/assets/scss/mixin';
.header-info {
  display: flex;
  align-items: center;

  .operation {
    display: inline-flex;
    margin-right: 16px;

    span {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 40px;
      height: 35px;

      &:hover {
        background: #f2f2f2;
      }

      i {
        font-size: 20px;
      }

      .dot {
        position: absolute;
        top: 3px;
        right: 3px;
        z-index: 10;
        width: 6px;
        height: 6px;
        background: red;
        border-radius: 100%;
      }
    }
  }

  .user-info {
    display: flex;
    align-items: center;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;

    &:focus-visible {
      background: #f2f2f2;
      outline: none;
    }

    .name {
      margin-left: 8px;
      @include font(16px, 600, #666);
    }
  }

  .info {
    // 因为el-dropdown是渲染在app.vue中，所以需要使用:global()来修改样式
    :global(.el-dropdown-menu__item) {
      line-height: 36px !important;
      padding: 6px 22px;
    }
  }
}
</style>
